package com.sam.im.heiqun.uis.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.sam.im.heiqun.R;
import com.sam.im.heiqun.app.App;
import com.sam.im.heiqun.entities.RequestRefereeEntity;
import com.sam.im.heiqun.entities.ValidateEntivity;
import com.sam.im.heiqun.nets.PGService;
import com.sam.im.heiqun.uis.beans.NoticeBean;
import com.sam.im.heiqun.utils.ToolsUtils;
import com.sam.im.heiqun.view.PullToRefreshListener;
import com.sam.im.heiqun.view.PullToRefreshRecyclerView;
import com.yuyh.library.nets.callbacks.AbsAPICallback;
import com.yuyh.library.nets.exceptions.ApiException;
import com.yuyh.library.uis.activitys.BaseSwipeBackActivity;

import butterknife.OnClick;

/**
 * Created by wangguodong on 2017/8/17.
 */

public class GroupNoticeEditActivity extends BaseSwipeBackActivity  {

    private static final String TAG = "GroupNoticeListActivity";

    String groupId ;

    private TextView ok ;
    private ImageView pre_v_back ;
    private EditText edit_title ;
    private EditText edit_content ;

    private int type = 0 ;//0-发布；1-编辑;2-查看（没有权修改）
    private NoticeBean noticeBean ;

    public static void startActivity(Activity activity, String groupId){
        Intent intent = new Intent(activity,GroupNoticeEditActivity.class);
        intent.putExtra("groupId",groupId);
        activity.startActivity(intent);
    }

    public static void startActivity(Activity activity, String groupId,int type, NoticeBean noticeBean){
        Intent intent = new Intent(activity,GroupNoticeEditActivity.class);
        intent.putExtra("groupId",groupId);
        intent.putExtra("type",type);
        intent.putExtra("noticeBean",noticeBean);
        activity.startActivity(intent);
    }

    @Override
    protected String getTitleText() {
        type = getIntent().getIntExtra("type", 0);
        if (2 ==type){
            return getResources().getString(R.string.detail);
        }else {
            return getResources().getString(R.string.edit);
        }
    }
    @Override
    protected void initViews(Bundle savedInstanceState) {

        groupId = getIntent().getStringExtra("groupId");
//        type = getIntent().getIntExtra("type", 0);
        noticeBean = (NoticeBean) getIntent().getSerializableExtra("noticeBean");

        edit_title = (EditText) findViewById(R.id.edit_title);
        edit_content = (EditText) findViewById(R.id.edit_content);
        pre_v_back = (ImageView) findViewById(R.id.pre_v_back);
        ok = (TextView) findViewById(R.id.ok);
        ok.setText(getResources().getString(R.string.finish));

        if ((1 ==type || 2 ==type) && null!=noticeBean){
            edit_title.setText(noticeBean.getTitle());
            edit_content.setText(noticeBean.getContent());
            if (2 ==type){
                edit_title.setFocusable(false);
                edit_content.setFocusable(false);
                ok.setVisibility(View.GONE);
            }else {
                ok.setVisibility(View.VISIBLE);
            }
        }else {
            ok.setVisibility(View.VISIBLE);
        }

    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_group_notice_edit;
    }


    private void createGroupNote(String content, String title){
        Log.i(TAG, "getNotice: ----------------------"+ToolsUtils.getMyUserId());//17231
        Log.i(TAG, "getNotice: ----------------------"+groupId);//2248
        PGService.getInstance().createGroupNote(ToolsUtils.getMyUserId(), groupId, content, title).subscribe(new AbsAPICallback<ValidateEntivity>() {
            @Override
            public void onNext(ValidateEntivity loginEntivity) {//
//   http://123.57.47.19:8080/wmsMobile/chat/createGroupNote?userId=17231&groupId=2248&content=嘿嘿嘿嘿额诶诶嘿嘿&title=呵呵呵
                showToast(loginEntivity.getInfo());
                finish();
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                showToast(getResources().getString(R.string.caozuo_fail));
            }

            @Override
            protected void onResultError(ApiException ex) {

                showToast(getResources().getString(R.string.caozuo_fail));
            }
        });
    }


    private void updateGroupNote(String content, String title, String noteId){
        Log.i(TAG, "updateGroupNote: ----------------------"+ToolsUtils.getMyUserId());//17231
        Log.i(TAG, "updateGroupNote: ----------------------"+groupId);//2248
        PGService.getInstance().updateGroupNote(ToolsUtils.getMyUserId(), groupId, noteId, content, title).subscribe(new AbsAPICallback<ValidateEntivity>() {
            @Override
            public void onNext(ValidateEntivity loginEntivity) {//
//   http://123.57.47.19:8080/wmsMobile/chat/createGroupNote?userId=17231&groupId=2248&content=嘿嘿嘿嘿额诶诶嘿嘿&title=呵呵呵
                showToast(loginEntivity.getInfo());
                finish();
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                showToast(getResources().getString(R.string.caozuo_fail));
            }

            @Override
            protected void onResultError(ApiException ex) {
                showToast(getResources().getString(R.string.caozuo_fail));
            }
        });
    }

    @OnClick({R.id.ok, R.id.pre_v_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ok: {
                String content = edit_content.getText().toString();
                String title = edit_title.getText().toString();

                if (null==title || "".equals(title)){
                    showToast(getResources().getString(R.string.input_notice_title));
                }else if (null==content || "".equals(content)){
                    showToast(getResources().getString(R.string.input_notice_content));
                }else {
                    if (0 == type){
                        createGroupNote(content, title);
                    }else if (1 == type){
                        updateGroupNote(content, title, noticeBean.getId());
                    }else {
                        showToast(getResources().getString(R.string.send_notice_qzgl));
                    }
                }
            }
                break;
            case  R.id.pre_v_back:
                finish();
                break;
        }
    }

}
